package net.vengeancecraft.NoPortals;

import java.util.Iterator;
import java.util.logging.Logger;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.world.PortalCreateEvent;


    public class WorldEvents implements Listener {
    
        public static final Logger log = Logger.getLogger("Minecraft");
        private FileHandler eLogger = new FileHandler();
        
        public WorldEvents () 
        {
           
        }
        
    
        @EventHandler(priority = EventPriority.LOWEST)
        public void onPortalCreate (PortalCreateEvent event)
        {
             if (event.isCancelled()){return;}
             
             World world = event.getWorld();
             if(world.getEnvironment() != Environment.THE_END)
             {
                if(!NoPortals.togglePortals)
                {

                    event.setCancelled(true);
                }
                else
                {
                    String  blockLocation;
                    for(Iterator iterator = event.getBlocks().iterator(); iterator.hasNext();)
                    {

                        Block block = (Block)iterator.next();

                        blockLocation = block.getLocation().getWorld().getName().toString() + "|" + Integer.toString(block.getLocation().getBlockX()) + "|" + Integer.toString(block.getLocation().getBlockY()) + "|" + Integer.toString(block.getLocation().getBlockZ());
                        if(!NoPortals.validPortals.containsKey(blockLocation))
                        {

                           NoPortals.validPortals.put(blockLocation, Boolean.TRUE);
                           eLogger.writeToFile("ValidPortals", blockLocation);
                        }
                    }
                    NoPortals.togglePortals = false;

                }
             }
        }       
}